<style>
  .buyCash, .walletSelect {

    border: #999 1px solid;
    text-align: center;
    color: #333;
    padding: 8px 22px;
    background-color: #fff;
    border-radius: 4px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    -webkit-box-shadow: 0px 1px 3px #fff;
    -moz-box-shadow: 0px 1px 3px #fff;
    box-shadow: 0px 1px 3px #fff;
    text-shadow: 1px 1px 3px #fff;
    text-decoration: none;
    outline: none;
  }

  .buyCash:hover {
    background-color: #18cca0;
    border-color: #18cca0;
    color: #fff;
  }

  #sumCart {
    font-size: 8px;
    color: red
  }
</style>

<div id="memberMain">
  <div class="title"><h4>积分商品</h4></div>
  <div class="height18"></div>
  <div class="memberList clearfix">
    <div class="tableClass">
      <table class="EmailTable center" cellspacing="0" cellpadding="0">
        <tbody>
        <tr class="">
          <td><input class="checkbox_all" type="checkbox" onchange="checkAll.call(this)">全选</td>
          <td width="30%">图片</td>
          <td width="30%">商品信息</td>
          <td></td>
          <td width="12%">金额</td>
          <td>操作</td>
        </tr>
        <tr class="" ng-repeat="obj in datalist" data-tid="{{obj.tid}}">
          <td>
            <input class="checkbox_one" type="checkbox" onchange="checkOne.call(this)">
          </td>
          <td>
            <img ng-src="{{'images/shop/'+obj.glPic}}" style="width: 72px;height: 72px">
          </td>
          <td>
            <span class="productInfo">{{obj.glName}}</span>
          </td>
          <td></td>
          <td>
            <div class="final_price">
              ￥:<b class="total_price" data-price="{{obj.glPrice}}">{{obj.glPrice | number : 2 }}</b>
            </div>
          </td>
          <td>
            <a href="javascript:void(0)" class="del" onclick="del_cart(this)">
              删除
            </a>
          </td>
        </tr>
        </tbody>
      </table>
      <div style="float: right;display: block;padding: 12px 8px">
        <span style="font-size: 16px">总计：<b id="sumCart">0</b></span>
        <a id="createAddr" ui-sref="transaction.center.mailpost" style="display: none;">添加收件地址</a>
        <select id="mailPostId" class="walletSelect" style="display: none;">
          <option ng-repeat="obj in addList" value="{{obj.tid}}">{{obj.raAddr}}</option>
        </select>
        <select id="wallet" class="walletSelect">
          <option value="ecoin" selected="">电子积分({{cpElecPoint}})</option>
        </select>
        <button type="button" class="buyCash" onclick="buySum()">付款</button>
      </div>
      <div class="height18"></div>
      <!--分页-->
      <!--<nav style="margin-left: 20px;">
        <ul class="pagination">
          <li>
            <a ng-click="Previous()">
              <span>上一页</span>
            </a>
          </li>
          <li ng-repeat="page in pageList" ng-class="{active: isActivePage(page)}" >
            <a ng-click="selectPage(page)" >{{ page }}</a>
          </li>
          <li>
            <a ng-click="Next()">
              <span>下一页</span>
            </a>
          </li>
        </ul>
      </nav>
      -->
      <!--分页-->
    </div>
  </div>
</div>
<script type="text/javascript">

  var priceCount = 0;

  function checkAll() {
    priceCount = 0;
    if (this.checked === true) {
      $('input:checkbox').each(function () {
        this.checked = true;
        price = parseFloat($(this).parent().parent().find('.total_price').attr('data-price'));
        if (!price) {
          price = 0;
        }
        priceCount = priceCount + price;
        $('#sumCart').html(priceCount)
      })
    } else {
      $('input:checkbox').each(function () {
        this.checked = false;
        priceCount = 0;
        $('#sumCart').html(priceCount)
      })
    }
  }

  function checkOne() {
    if (this.checked === true) {
      price = parseFloat($(this).parent().parent().find('.total_price').attr('data-price'));
      priceCount = priceCount + price;
      $('#sumCart').html(priceCount);
      var len = $('input.checkbox_one').length;
      var checkedNum = 0;
      for (var i = 0; i < len; i++) {
        if ($('input.checkbox_one')[i].checked == true) {
          checkedNum++;
        }
      }
      $('.checkbox_all')[0].checked = len == checkedNum ? true : false;
    } else {
      price = parseFloat($(this).parent().parent().find('.total_price').attr('data-price'));
      priceCount = priceCount - price;
      $('#sumCart').html(priceCount);
      $('.checkbox_all')[0].checked = false;
    }
  }

  function del_cart(e) {
    $('body').mLoading('show');
    $.ajax({
      url: '/goods/delChart',
      data: {
        purchaseid: $(e).parent().parent().attr('data-tid')
      },
      type: 'post',
      success: function (json) {
        $('body').mLoading('hide');
        if (json.retCode == 1) {
          $(e).parent().parent().remove();
        } else {
          alert('删除失败。。');
        }
      }
    });
  }

  function buySum() {
    //检测是否有收货地址
    var checkHasAddr = $('#createAddr').css('display');
    if (priceCount == 0) {
      alert('请先选择商品!');
      return;
    }
    if (checkHasAddr == 'block') {
      alert('请添加收货地址！');
      return;
    }
    $('body').mLoading('show');
    list = [];
    $('input:checkbox').each(function () {
      if (this.checked == true) {
        list.push($(this).parent().parent().data('tid'))
      }
    });
    var postId = list.length == 1 ? list[0] : list.join(',');


    var investing = false;
    $.ajax({
      type: "post",
      url: 'goods/checkInvest',
      data: {},
      async: false,
      success: function (result) {
        if (result.retCode == '1') {
          investing = result.data.investing;
        }
      }
    });
    if (investing) {
      $('body').mLoading('hide');
      confirm('存在投资中项目，不可重复投资，确认付款？', function () {
        addOrderAct(postId)
      }, function () {
        $('body').mLoading('hide');
      })
    } else {
      addOrderAct(postId)
    }
  }

  function addOrderAct(postId) {
    $.ajax({
      url: '/purchase/addOrder',
      data: {
        'purchaseId': postId,
        'raAddr': $('#mailPostId').val()
      },
      type: 'post',
      success: function (json) {
        $('body').mLoading('hide');
        if (json.retCode == 1) {
          window.location.href = '#!/transaction/center/order';
        } else {
          alert(json.retMsg);
        }
      }
    });
  }
</script>
